package com.mes.web.controller.system;

import java.util.List;
import java.util.HashMap;
import java.util.Map;
import com.mes.common.annotation.Log;
import com.mes.common.core.domain.R;
import com.github.pagehelper.PageInfo;
import com.mes.common.core.page.PageDomain;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.mes.common.enums.BusinessType;
import com.mes.common.utils.poi.ExcelUtil;
import com.mes.framework.cache.DictUtils;
import javax.validation.constraints.*;
import org.springframework.web.bind.annotation.*;
import com.mes.common.core.controller.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import com.mes.system.domain.SysTenant;
import com.mes.system.service.SysTenantService;

/**
 * 租户Controller
 * @author hwg
 * @email hwg
 * @date 2023-03-25
 */
@RestController
@RequestMapping("/system/sysTenant")
public class SysTenantController extends BaseController {

    @Autowired
    private SysTenantService sysTenantService;

    /**
     * 查询租户列表
     */
    @PreAuthorize("@ss.hasPermi('system:sysTenant:list')")
    @GetMapping("/list")
    public R<PageInfo> list(SysTenant sysTenant, HttpServletRequest request, HttpServletResponse response) {
        sysTenant.setPage(new PageDomain(request, response));
        return R.data(sysTenantService.findPage(sysTenant));
    }

    /**
     * 获取租户详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:sysTenant:query')")
    @GetMapping(value = "/{id}")
    public R<SysTenant> detail(@PathVariable("id") String id) {
        return R.data(sysTenantService.get(id));
    }

    /**
     * 新增租户
     */
    @PreAuthorize("@ss.hasPermi('system:sysTenant:add')")
    @Log(title = "租户", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated  SysTenant sysTenant) {
        return R.status(sysTenantService.create(sysTenant));
    }

    /**
     * 修改租户
     */
    @PreAuthorize("@ss.hasPermi('system:sysTenant:edit')")
    @Log(title = "租户", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated SysTenant sysTenant) {
        return R.status(sysTenantService.save(sysTenant));
    }

    /**
     * 更新租户状态
     */
    @PreAuthorize("@ss.hasPermi('system:sysTenant:edit')")
    @Log(title = "租户", businessType = BusinessType.UPDATE)
    @PutMapping("/updateStatus")
    public R updateStatus(@RequestBody SysTenant sysTenant) {
        return R.status(sysTenantService.updateStatus(sysTenant));
    }

    /**
     * 删除租户
     */
    @PreAuthorize("@ss.hasPermi('system:sysTenant:remove')")
    @Log(title = "租户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.status(sysTenantService.deleteSysTenantByIds(ids));
    }


    /**
     * 导出租户列表
     */
    @PreAuthorize("@ss.hasPermi('system:sysTenant:export')")
    @Log(title = "租户", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(SysTenant sysTenant) {
        List<SysTenant> list = sysTenantService.findList(sysTenant);
        ExcelUtil<SysTenant> util = new ExcelUtil<SysTenant>(SysTenant.class);
        return util.exportExcel(list, "租户数据");
    }

    /**
     * 根据字典类型查询字典数据信息等其他自定义信息
     */
    @GetMapping(value = "/getInitData/{dictTypes}")
    public R getInitData(@PathVariable String dictTypes) {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.putAll(DictUtils.getMultiDictList(dictTypes));
        return R.data(dataMap);
    }

}
